package com.navinfo.platform.common.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class ConnectionUtil {
    private String url;
    private String user;
    private String password;
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    private volatile Map<String, String> configMap;

    private ConnectionUtil(){

    }
    private static final class InstanceHolder {
        private static final ConnectionUtil INSTANCE = new ConnectionUtil();
    }

    public static ConnectionUtil getInstance(){
        return InstanceHolder.INSTANCE;
    }

    public void init(Map<String, String> configMap){
        this.configMap = configMap;
        this.user = configMap.get("mysql.user");
        this.password = configMap.get("mysql.password");
        this.url = configMap.get("mysql.url");
    }

    public Connection connect(){
        Connection conn = null;
        //connect db
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            System.out.println("connect failed!");
            e.printStackTrace();
        }
        return conn;
    }



}
